/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.put.dcs_modeler.wizards.newComputingObject;

import java.awt.Component;
import java.beans.PropertyVetoException;
import java.lang.String;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import java.util.logging.Logger;
import javax.swing.ComboBoxEditor;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import org.netbeans.api.project.Project;
import org.netbeans.api.project.ProjectUtils;
import org.netbeans.spi.project.AuxiliaryConfiguration;
import org.openide.WizardDescriptor;
import org.openide.util.Exceptions;
import org.w3c.dom.Element;
import pl.put.dcs_modeler.utils.DCSProjectUtil;
import pl.put.dcs_modeler.utils.JarUtil;

public final class NewComputingObjectVisualPanel2 extends JPanel
{

    private static Logger log = Logger.getLogger( NewComputingObjectVisualPanel2.class.getName() );
    private Project project;

    /** Creates new form NewComputingObjectVisualPanel2 */
    public NewComputingObjectVisualPanel2( )
    {
        initComponents();
        
    }

    private void initTables()
    {
        //tabela 1 - in
        int vColIndex = 1;
        TableColumn col = jTable1.getColumnModel().getColumn( vColIndex );
        String[] values = getDataModelClasses();
        col.setCellEditor( new DataTypeEditor( values ) );
        col.setCellRenderer( new DataTypeRenderBox( values ) );
        // tabela nr2 (out)
        col = jTable2.getColumnModel().getColumn( vColIndex );
        values = getDataModelClasses();
        col.setCellEditor( new DataTypeEditor( values ) );
        col.setCellRenderer( new DataTypeRenderBox( values ) );

    }
    
    @Override
    public String getName()
    {
        return "Pins";
    }

    public boolean isValid( WizardDescriptor wd )
    {
        if ( validateTable( jTable1 ) && validateTable( jTable2 ) ) // po co zaprzeczac wyrazenia. Mniej wydajny algorytm ;)
        {
            wd.putProperty( "WizardPanel_errorMessage", "" );
            return true;
        }
        
        wd.putProperty( "WizardPanel_errorMessage", org.openide.util.NbBundle.getMessage(NewComputingObjectVisualPanel2.class, "Pin_must_have_name_and_type._Check_pin_list.") );
        log.info( "dupa1");
        return false;
    }

    private boolean validateTable( JTable table )
    {
        Vector< Vector< String > > pins = ( ( DefaultTableModel ) table.getModel() ).getDataVector();
        if ( pins.size() > 0 )
        {
            for ( Vector pin : pins )
            {
                if ( ( ( String )pin.elementAt( 0 ) == null ) || ( ( ( String )pin.elementAt( 0 ) ).equals( "" ) )
                        || ( ( String )pin.elementAt( 1 ) == null ) || ( ( ( String )pin.elementAt( 1 ) ).equals( "" ) ) )
                {
                    log.info( "false");
                    return false;

                }
            }
        }
        log.info( "true");
        return true;
    }

    public void readSettings( WizardDescriptor d )
    {
        this.project = ( Project )d.getProperty( "project" );
        initTables();
    }

    void storeSettings( WizardDescriptor d )
    {
        d.putProperty( "inPins", ( ( DefaultTableModel ) jTable1.getModel() ).getDataVector() );
        d.putProperty( "outPins", ( ( DefaultTableModel ) jTable2.getModel() ).getDataVector() );
    }

    private String[] getDataModelClasses()
    {
        String dmPath = DCSProjectUtil.getDataModelPath( project );
        List< String > ret = JarUtil.getJarClassList( dmPath );
        return ( String[] )ret.toArray( new String[ 0 ] );
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        addPinDialog = new javax.swing.JDialog();
        jLabel4 = new javax.swing.JLabel();
        pinName = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        typeCombo = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        dmPath = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        addInButton = new javax.swing.JButton();
        deleteInButton = new javax.swing.JButton();
        addOutPin = new javax.swing.JButton();
        deleteOutPin = new javax.swing.JButton();

        org.openide.awt.Mnemonics.setLocalizedText(jLabel4, org.openide.util.NbBundle.getMessage(NewComputingObjectVisualPanel2.class, "NewComputingObjectVisualPanel2.jLabel4.text")); // NOI18N

        pinName.setText(org.openide.util.NbBundle.getMessage(NewComputingObjectVisualPanel2.class, "NewComputingObjectVisualPanel2.pinName.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel5, org.openide.util.NbBundle.getMessage(NewComputingObjectVisualPanel2.class, "NewComputingObjectVisualPanel2.jLabel5.text")); // NOI18N

        typeCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                typeComboActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(jLabel6, org.openide.util.NbBundle.getMessage(NewComputingObjectVisualPanel2.class, "NewComputingObjectVisualPanel2.jLabel6.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(dmPath, org.openide.util.NbBundle.getMessage(NewComputingObjectVisualPanel2.class, "NewComputingObjectVisualPanel2.dmPath.text")); // NOI18N

        javax.swing.GroupLayout addPinDialogLayout = new javax.swing.GroupLayout(addPinDialog.getContentPane());
        addPinDialog.getContentPane().setLayout(addPinDialogLayout);
        addPinDialogLayout.setHorizontalGroup(
            addPinDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(addPinDialogLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(addPinDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(addPinDialogLayout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addGap(10, 10, 10)
                        .addComponent(typeCombo, 0, 397, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, addPinDialogLayout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(pinName, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE))
                    .addGroup(addPinDialogLayout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(dmPath, javax.swing.GroupLayout.DEFAULT_SIZE, 346, Short.MAX_VALUE)))
                .addContainerGap())
        );
        addPinDialogLayout.setVerticalGroup(
            addPinDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, addPinDialogLayout.createSequentialGroup()
                .addContainerGap(22, Short.MAX_VALUE)
                .addGroup(addPinDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(dmPath, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(addPinDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(pinName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(addPinDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(typeCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addContainerGap())
        );

        setPreferredSize(new java.awt.Dimension(619, 445));

        org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(NewComputingObjectVisualPanel2.class, "NewComputingObjectVisualPanel2.jLabel1.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(NewComputingObjectVisualPanel2.class, "NewComputingObjectVisualPanel2.jLabel2.text")); // NOI18N

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "name", "type"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jTable1.setColumnSelectionAllowed(true);
        jTable1.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                jTable1PropertyChange(evt);
            }
        });
        jScrollPane2.setViewportView(jTable1);
        jTable1.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jTable1.getColumnModel().getColumn(0).setPreferredWidth(50);
        jTable1.getColumnModel().getColumn(0).setHeaderValue(org.openide.util.NbBundle.getMessage(NewComputingObjectVisualPanel2.class, "NewComputingObjectVisualPanel2.jTable1.columnModel.title0")); // NOI18N
        jTable1.getColumnModel().getColumn(1).setHeaderValue(org.openide.util.NbBundle.getMessage(NewComputingObjectVisualPanel2.class, "NewComputingObjectVisualPanel2.jTable1.columnModel.title1")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel3, org.openide.util.NbBundle.getMessage(NewComputingObjectVisualPanel2.class, "NewComputingObjectVisualPanel2.jLabel3.text")); // NOI18N

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "name", "type"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jTable2.setColumnSelectionAllowed(true);
        jTable2.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                jTable2PropertyChange(evt);
            }
        });
        jScrollPane1.setViewportView(jTable2);
        jTable2.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jTable2.getColumnModel().getColumn(0).setPreferredWidth(50);
        jTable2.getColumnModel().getColumn(0).setHeaderValue(org.openide.util.NbBundle.getMessage(NewComputingObjectVisualPanel2.class, "NewComputingObjectVisualPanel2.jTable2.columnModel.title0")); // NOI18N
        jTable2.getColumnModel().getColumn(1).setHeaderValue(org.openide.util.NbBundle.getMessage(NewComputingObjectVisualPanel2.class, "NewComputingObjectVisualPanel2.jTable2.columnModel.title1")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(addInButton, org.openide.util.NbBundle.getMessage(NewComputingObjectVisualPanel2.class, "NewComputingObjectVisualPanel2.addInButton.text")); // NOI18N
        addInButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addInButtonActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(deleteInButton, org.openide.util.NbBundle.getMessage(NewComputingObjectVisualPanel2.class, "NewComputingObjectVisualPanel2.deleteInButton.text")); // NOI18N
        deleteInButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteInButtonActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(addOutPin, org.openide.util.NbBundle.getMessage(NewComputingObjectVisualPanel2.class, "NewComputingObjectVisualPanel2.addOutPin.text")); // NOI18N
        addOutPin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addOutPinActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(deleteOutPin, org.openide.util.NbBundle.getMessage(NewComputingObjectVisualPanel2.class, "NewComputingObjectVisualPanel2.deleteOutPin.text")); // NOI18N
        deleteOutPin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteOutPinActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 595, Short.MAX_VALUE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addComponent(addInButton)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(deleteInButton))
                        .addComponent(jLabel3))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 599, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(addOutPin)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(deleteOutPin)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(deleteInButton)
                    .addComponent(addInButton))
                .addGap(9, 9, 9)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(deleteOutPin)
                    .addComponent(addOutPin))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

private void addInButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addInButtonActionPerformed
//    setDataTypeDialogCombo();
    Vector addVec = new Vector();

    // addVec.add( new JComboBox( new String[]{ "llll", "dssdsds", "1111111" } ) );

    ( ( DefaultTableModel )jTable1.getModel() ).addRow( addVec );
    //AddPinDialog add = new AddPinDialog( new Frame(), false );
    fireVetoableChange();
 
}//GEN-LAST:event_addInButtonActionPerformed

private void deleteInButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteInButtonActionPerformed

    try
    {
        ( ( DefaultTableModel )jTable1.getModel() ).removeRow( jTable1.getSelectedRow() );
        fireVetoableChange();
    }
    catch ( Exception e )
    {
    }
    
}//GEN-LAST:event_deleteInButtonActionPerformed

private void typeComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_typeComboActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_typeComboActionPerformed

private void deleteOutPinActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_deleteOutPinActionPerformed
{//GEN-HEADEREND:event_deleteOutPinActionPerformed
    try
    {
        ( ( DefaultTableModel )jTable2.getModel() ).removeRow( jTable2.getSelectedRow() );
        fireVetoableChange();
    }
    catch ( Exception e )
    {
    }
   
}//GEN-LAST:event_deleteOutPinActionPerformed

private void addOutPinActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_addOutPinActionPerformed
{//GEN-HEADEREND:event_addOutPinActionPerformed
    ( ( DefaultTableModel )jTable2.getModel() ).addRow( new Vector() );
    fireVetoableChange();
}//GEN-LAST:event_addOutPinActionPerformed

private void jTable1PropertyChange(java.beans.PropertyChangeEvent evt)//GEN-FIRST:event_jTable1PropertyChange
{//GEN-HEADEREND:event_jTable1PropertyChange
    //log.info( "property change" );
    fireVetoableChange();
}//GEN-LAST:event_jTable1PropertyChange

private void jTable2PropertyChange(java.beans.PropertyChangeEvent evt)//GEN-FIRST:event_jTable2PropertyChange
{//GEN-HEADEREND:event_jTable2PropertyChange
    fireVetoableChange();
}//GEN-LAST:event_jTable2PropertyChange

private void fireVetoableChange()
{
    try
    {
        fireVetoableChange( "", null, "" );
        //AddPinDialog add = new AddPinDialog( new Frame(), false );
    }
    catch ( PropertyVetoException ex )
    {
        Exceptions.printStackTrace( ex );
    }
}

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addInButton;
    private javax.swing.JButton addOutPin;
    private javax.swing.JDialog addPinDialog;
    private javax.swing.JButton deleteInButton;
    private javax.swing.JButton deleteOutPin;
    private javax.swing.JLabel dmPath;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    private javax.swing.JTextField pinName;
    private javax.swing.JComboBox typeCombo;
    // End of variables declaration//GEN-END:variables

    private class DataTypeEditor extends DefaultCellEditor
    {

        public DataTypeEditor( String[] dataTypesArray )
        {
            super( new JComboBox( dataTypesArray ) );
        }
    }

    private class DataTypeRenderBox extends JComboBox implements TableCellRenderer
    {

        public DataTypeRenderBox( String[] items )
        {
            super( items );

        }

        @Override
        public Component getTableCellRendererComponent( JTable table, Object value,
                                                        boolean isSelected, boolean hasFocus, int row, int column )
        {
            if ( isSelected )
            {
                setForeground( table.getSelectionForeground() );
                super.setBackground( table.getSelectionBackground() );
            }
            else
            {
                setForeground( table.getForeground() );
                setBackground( table.getBackground() );
            }
          
           
            
            setSelectedItem( ( String )value );
            return this;
        }
    }
}

